Modified from original
x
to setup clear-all ; add paticles create-turtles number-of-particles [ set shape "circle" set size 0.5 setxy (min-pxcor + 5) 0 set color red fd random-float 1 ] if types-of-particles = 2 [ ask n-of int (number-of-particles / 2) turtles [ set color yellow ] ] ; make chambers ask patches with [pxcor < 2 and pxcor > -2] [set pcolor blue] set diffusion-coefficient 0.5 reset-ticksendto random-walk ; take a step forward through fractional distance less than 1 fd random-float diffusion-coefficient ;randomize direction (heading) for the next step rt random 360endto go ifelse trace-path? [ ask turtles [pen-down] ] [ ask turtles [pen-up] ] ask turtles [ ; if you are near a blue wall or an edge , turn back, else do the random walk if patch-ahead (int diffusion-coefficient + 1)= nobody [ set heading 90 fd 1 ] if [pcolor] of patch-ahead (int diffusion-coefficient + 1) = blue [ set heading 270 fd 1 ] random-walk ; additional movement depending on the charge if (color = read-from-string color-with-positive-charge) [ if (xcor + strength-of-the-field) > min-pxcor [ ; to make sure that the particle does not move beyod the edge of the world set xcor (xcor + strength-of-the-field) ] ] if (color = read-from-string color-with-negative-charge) [ if (xcor + strength-of-the-field) > min-pxcor [ ; to make sure that the particle does not move beyod the edge of the world set xcor (xcor - strength-of-the-field) ] ] ] tickend